/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function mark(albumId, figureId) {
    showLoading();
    new Ajax.Request('MarkFigureServlet?albumId='+albumId+'&figureId='+figureId,
    {
        method: 'get',
        requestHeaders: {
            Accept: 'application/xml'
        },
        asynchronous: false,
        onSuccess: function (transport) {
            //alert(transport.responseText);
            var xml = transport.responseXML;
            if (xml != null && xml != '') {
                var result = xml.getElementsByTagName('result').item(0).firstChild.data;
                if (result == 'success') {
                    $('td_' + albumId + '@' + figureId).style.background = '#AEAEAE';
                    $('td_' + albumId + '@' + figureId).style.fontWeight = 'bold';
                    $('ahref_'+ albumId + '@' + figureId).href = 'javascript:unmark(' + albumId + ', ' + figureId + ');';
                    $('owned').innerHTML = (Number($('owned').innerHTML) + 1);
                    $('missing').innerHTML = (Number($('total').innerHTML) - Number($('owned').innerHTML));
                    closeLoading();
                }
                else {
                    alert('Error');
                    closeLoading();
                }
            }
            else {
                alert('Error');
                closeLoading();
            }
            closeLoading();
        },
        onComplete: function () {
            closeLoading();
        },
        onFailure: function () {
            alert('Error');
            closeLoading();
        }
    });
}
function unmark(albumId, figureId) {
    showLoading();
    new Ajax.Request('UnmarkFigureServlet?albumId='+albumId+'&figureId='+figureId,
    {
        method: 'get',
        requestHeaders: {
            Accept: 'application/xml'
        },
        asynchronous: false,
        onSuccess: function (transport) {
            //alert(transport.responseText);
            var xml = transport.responseXML;
            if (xml != null && xml != '') {
                var result = xml.getElementsByTagName('result').item(0).firstChild.data;
                if (result == 'success') {
                    $('td_' + albumId + '@' + figureId).style.background = '#FFFFFF';
                    $('td_' + albumId + '@' + figureId).style.fontWeight = 'normal';
                    $('ahref_'+ albumId + '@' + figureId).href = 'javascript:mark(' + albumId + ', ' + figureId + ');';
                    $('owned').innerHTML = (Number($('owned').innerHTML) - 1);
                    $('missing').innerHTML = (Number($('total').innerHTML) - Number($('owned').innerHTML));
                    closeLoading();
                }
                else {
                    alert('Error');
                    closeLoading();
                }
            }
            else {
                alert('Error');
                closeLoading();
            }
            closeLoading();
        },
        onComplete: function () {
            closeLoading();
        },
        onFailure: function () {
            alert('Error');
            closeLoading();
        }
    });
}

function repeatedMark(albumId, figureId) {
    showLoading();
    var td  = $('td_' + albumId + '@' + figureId);
    if (td.style.fontWeight != 'bold') {
        alert(notHaveFigureInAlbum);
        closeLoading();
        return;
    }
    new Ajax.Request('MarkRepeatedFigureServlet?albumId='+albumId+'&figureId='+figureId,
    {
        method: 'get',
        requestHeaders: {
            Accept: 'application/xml'
        },
        asynchronous: false,
        onSuccess: function (transport) {
            //alert(transport.responseText);
            var xml = transport.responseXML;
            if (xml != null && xml != '') {
                var result = xml.getElementsByTagName('result').item(0).firstChild.data;
                if (result == 'success') {
                    $('rtd_' + albumId + '@' + figureId).style.background = '#AEAEAE';
                    $('rtd_' + albumId + '@' + figureId).style.fontWeight = 'bold';
                    $('rahref_'+ albumId + '@' + figureId).href = 'javascript:repeatedUnmark(' + albumId + ', ' + figureId + ');';
                    $('repeated').innerHTML = (Number($('repeated').innerHTML) + 1);
                    closeLoading();
                }
                else {
                    alert('Error');
                    closeLoading();
                }
            }
            else {
                alert('Error');
                closeLoading();
            }
            closeLoading();
        },
        onComplete: function () {
            closeLoading();
        },
        onFailure: function () {
            alert('Error');
            closeLoading();
        }
    });
}
function repeatedUnmark(albumId, figureId) {
    showLoading();
    new Ajax.Request('UnmarkRepeatedFigureServlet?albumId='+albumId+'&figureId='+figureId,
    {
        method: 'get',
        requestHeaders: {
            Accept: 'application/xml'
        },
        asynchronous: false,
        onSuccess: function (transport) {
            //alert(transport.responseText);
            var xml = transport.responseXML;
            if (xml != null && xml != '') {
                var result = xml.getElementsByTagName('result').item(0).firstChild.data;
                if (result == 'success') {
                    $('rtd_' + albumId + '@' + figureId).style.background = '#FFFFFF';
                    $('rtd_' + albumId + '@' + figureId).style.fontWeight = 'normal';
                    $('rahref_'+ albumId + '@' + figureId).href = 'javascript:repeatedMark(' + albumId + ', ' + figureId + ');';
                    $('repeated').innerHTML = (Number($('repeated').innerHTML) - 1);
                    closeLoading();
                }
                else {
                    alert('Error');
                    closeLoading();
                }
            }
            else {
                alert('Error');
                closeLoading();
            }
            closeLoading();
        },
        onComplete: function () {
            closeLoading();
        },
        onFailure: function () {
            alert('Error');
            closeLoading();
        }
    });
}